package qualomelhor.game;

import static playn.core.PlayN.graphics;
import playn.core.Image;
import playn.core.ImageLayer;

public class Mao {
	
	private ImageLayer layer;
	private Image imgMao;
	private int maoId;
	private int subMaoId;
	private float hipMedio;
	private float angleBeta;
	private float angleRotacao;
	private float width;
	private float height;
	private float posX;
	private float posY;
	private float angle;
	private float posXL;
	private float posYL;
	private float posXLAbs;
	private float posYLAbs;
	private int scaleX, scaleY;
	private float prevX, prevY, prevA;

	public Mao (int maoId, int subMaoId, float x, float y, float angle, Image imgMao, int  scX, int scY) {
		this.maoId = maoId;
		this.imgMao = imgMao;
		this.subMaoId = subMaoId;
		this.posX = x;
		this.posY = y;
		this.scaleX = scX;
		this.scaleY = scY;
		layer = graphics().createImageLayer(imgMao);
		setImage(imgMao);
	}

	public void update(float delta) {
		layer.setRotation(angle);
		layer.setTranslation(posX, posY);
		layer.setScale(scaleX, scaleY);
		/*
		prevX = posX;
	    prevY = posY;
	    prevA = angle;
		float x = (posX * alpha) + (prevX * (1f - alpha));
		float y = (posY * alpha) + (prevY * (1f - alpha));
		float a = (angle * alpha) + (prevA * (1f - alpha));
		layer.setRotation(a);
		layer.setTranslation(x, y);
		layer.setScale(scaleX * layer.transform().scaleX(), scaleY * layer.transform().scaleY());
		*/

	}

	public void paint(float alpha) {
		// interpolate based on previous state
	}

	public ImageLayer getLayer() {
		return layer;
	}

	public float getWidth() {
		return imgMao.width();
	}

	public float getHeight() {
		return imgMao.height();
	}

	public int getMaoId() {
		return maoId;
	}

	public int getSubMaoId() {
		return subMaoId;
	}

	public Image getImage() {
		return imgMao;
	}

	public float getHidMedio() {
		return hipMedio;
	}

	public float getAngleBeta() {
		return angleBeta;
	}

	public float getAngleRotacao() {
		return angleRotacao;
	}

	public float getPosX() {
		return posX;
	}

	public float getPosY() {
		return posY;
	}

	public float getPosXL() {
		return posXL;
	}

	public float getPosYL() {
		return posYL;
	}

	public float getPosXLAbs() {
		return posXLAbs;
	}

	public float getPosYLAbs() {
		return posYLAbs;
	}

	public void setImage(Image imgMao) {
		this.imgMao = imgMao;
		layer.setImage(this.imgMao);
		width = (int)imgMao.width();
		height = (int)imgMao.height();
		hipMedio = (float)Math.sqrt(Math.pow(width, 2) + Math.pow(height / 2, 2));
		angleBeta = (float)Math.atan(((float)height / 2) / ((float)width));
	}

	public void setMaoId(int maoId) {
		this.maoId = maoId;
	}

	public void setSubMaoId(int subMaoId) {
		this.subMaoId = subMaoId;
	}

	public void setPosition (float x, float y) {
		this.posX = x;
		this.posY = y;
	}

	public void setAngle(float angle) {
		this.angle = angle;
	}

	public void setAngleRotation(float angle) {
		if (Math.abs(angle) < 0.001) {
			this.angleRotacao = 0f;
		} else {
			this.angleRotacao = angle;
		}
		posXL = (float) (hipMedio * Math.cos(angleBeta + angleRotacao));
		posYL = (float) (hipMedio * Math.sin(angleBeta + angleRotacao));
		posXLAbs = (float) (hipMedio * Math.cos(angleBeta + Math.abs(angleRotacao)));
		posYLAbs = (float) (hipMedio * Math.sin(angleBeta + Math.abs(angleRotacao)));
	}
}